<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="${pageContext.request.contextPath}/js/jquery-1.8.3.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui/layui.js"></script>
</head>
<body>

<!-- 添加的form表单 star-->
<form class="layui-form" id="updateForm" lay-filter="updateFilter" style="display: none;padding: 10px 10px 0 0" method="post">
    <div class="layui-form-item">
        <label class="layui-form-label">管理员名</label>
        <div class="layui-input-inline">
            <input type="hidden" name="id">
            <input type="text" name="username" placeholder="请输入管理员名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-inline">
            <input type="text" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="update">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<!--添加表单end-->

<!-- 添加的form表单 star-->
<form class="layui-form" id="addForm" style="display: none;padding: 10px 10px 0 0" method="post">
    <div class="layui-form-item">
        <label class="layui-form-label">管理员名</label>
        <div class="layui-input-inline">
            <input type="text" name="username" placeholder="请输入管理员名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-inline">
            <input type="text" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色名字</label>
        <div class="layui-input-block">
            <select name="role_id" id="select1" lay-filter="category">
                <option value="1">admin</option>
                <option value="2">superadmin</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<!--添加表单end-->

<div>
    <button class="layui-btn" id="addBtn">添加</button>
</div>

<table id="demo" lay-filter="myTableFilter"></table>

<script type="text/html" id="toolbar1">
    <a class="layui-btn layui-btn-xs" lay-event="test1">修改</a>
    <a class="layui-btn layui-btn-xs" lay-event="test2">删除</a>
</script>

    <script>
        layui.use(['table','form'], function() {
            var table = layui.table;
            var form = layui.form;


            //渲染 table表格
            table.render({
                elem: '#demo'
                , url: '${pageContext.request.contextPath}/admin/selectPage' //数据接口
                , page: true //开启分页
                , limit: 5
                , limits: [3, 5, 10]
                , cols: [[ //表头
                      {field: 'id', title: '编号'}
                    , {field: 'username', title: '管理员名'}
                    , {field: 'password', title: '密码'}
                    , {field: 'role_name', title: '角色名'}
                    , {title: '操作', toolbar: '#toolbar1'}
                ]]
            });

            //添加弹出框start
            $("#addBtn").click(function () {
                layer.open({
                    type:1,
                    content:$("#addForm"),
                    area:'500px',
                    title:'添加管理员'
                })
            })
            //添加弹出框 end

            //添加 start
            form.on("submit(*)",function() {
                var s = $("#addForm").serialize();
                $.ajax({
                    url:"${pageContext.request.contextPath}/admin/add",
                    data:s,
                    dataType:"json",
                    success:function(result){
                        if(result.flag){
                            layer.closeAll();
                            layer.msg("添加成功");
                            $("#addForm")[0].reset();
                            table.reload('demo',{
                                //从第几页开始
                                page:{curr:1}
                            })
                        }
                    }
                })
                return false;
            })
            //添加 end

            //删除修改start
            table.on('tool(myTableFilter)',function (obj) {
                 if(obj.event=='test1'){
                    layer.open({
                        type:1,
                        title:'修改品牌信息',
                        area:['500px','400px'],
                        content:$("#updateForm")
                    })
                    //回显
                    $.ajax({
                        url:'${pageContext.request.contextPath}/admin/selectOne',
                        data:'id='+obj.data.id,
                        dataType:'json',
                        success:function(result){
                            form.val('updateFilter',{
                                id:result.id,
                                username:result.username,
                                password:result.password,
                            })
                        }
                    })
                 }else
                // 删除
                if(obj.event=='test2'){
                    //询问弹出框
                    layer.confirm('确定删除吗?',function(index){
                        $.ajax({
                            url:'${pageContext.request.contextPath}/admin/delete',
                            data:'id='+obj.data.id,
                            dataType:'json',
                            success:function(result){
                                if(result.flag){
                                    layer.msg('删除成功');
                                    table.reload('demo',{
                                        //从第几页开始
                                        page:{curr:1}
                                    })
                                }
                            }
                        })
                    })
                }
            })
            //删除end

            //修改  stear
            form.on("submit(update)",function() {
                var s = $("#updateForm").serialize();
                $.ajax({
                    url:"${pageContext.request.contextPath}/admin/update",
                    data:s,
                    dataType:"json",
                    success:function(result){
                        if(result.flag){
                            layer.closeAll();
                            layer.msg("修改成功");
                            table.reload('demo',{
                                //从第几页开始
                                page:{curr:1}
                            })
                        }
                    }
                })
                return false;
            })

        })
</script>
</body>
</html>
